const db = require("../core/mysql");
const moment = require("moment");

class CartController {
    async addCart(request, response, next) {
        try {
           
            let uparams = [
                request.Info.uid,
                request.body.spid
            ]
            let sql1 = 'SELECT * FROM `shopp` WHERE `uid`=? AND `spid`=?;';
            let result = await db.exec(sql1, uparams);
            if (result.length >= 1) {
                let uprams = [
                    Number(request.body.ssum),
                    request.Info.uid,
                    request.body.spid
                ]
               
                let usql = 'UPDATE `shopp` SET `ssum`=`ssum`+?,`stotal`=`sprice`*`ssum`,`sstart`="1"  WHERE `uid`=? AND `spid`=?;';
                let uresult = await db.exec(usql, uprams);
                if (uresult && uresult.affectedRows >= 1) {
                    response.json({
                        code: 200,
                        msg: "加入购物车成功a~~~"
                    })
                } else {
                    response.json({
                        code: -200,
                        msg: "加入购物车失败a~~~"
                    })
                }
            } else {
                let iparmas = [
                    request.body.sname,
                    request.body.sprice,
                    request.Info.uid,
                    request.body.spid,
                    request.body.simg,
                    request.body.ssum,
                    request.body.sprice * request.body.ssum,
                    moment().format("YYYY-MM-DD HH:mm:ss")
                ]
                let isql = 'INSERT INTO `shopp`(`sname`,`sprice`,`uid`,`spid`,`simg`,`ssum`,`stotal`,`sdate`) VALUE(?,?,?,?,?,?,?,?);';
                
                let iresult = await db.exec(isql, iparmas);
               
                if (iresult && iresult.affectedRows >= 1) {
                    response.json({
                        code: 200,
                        msg: "加入购物车成功b~~~"
                    })
                } else {
                    response.json({
                        code: -200,
                        msg: "加入购物车失败b~~~"
                    })
                }
            }
        } catch (error) {
            response.json({
                code: -200,
                msg: "加入购车失败c~~~",
                error
            })
        }
    }

    async getCart(request, response, next) {
        let params = [
            request.Info.uid
        ]
        let sql = 'SELECT * FROM `shopp` WHERE `uid`=? AND `sstart`="1"';
        let result = await db.exec(sql, params);
        try {
            if (result && result.length >= 1) {
                response.json({
                    code: 200,
                    msg: "查询成功~~~",
                    data: result
                })
            } else {
                response.json({
                    code: -200,
                    msg: "查询失败~~~"
                });
            }
        } catch (error) {
            response.json({
                code: -200,
                msg: "查询失败~~~",
                error
            });
        }

    }

    async updataCart(request, response, next) {
        let uparams = [
            request.body.ssum,
            request.body.sid
        ]
        let usql = 'update `shopp` set `ssum`=?,`stotal`=`sprice`*`ssum` where `sid`=? and `sstart`="1";';
        try {
            let uresult = await db.exec(usql, uparams);
            if (uresult && uresult.affectedRows > -1) {
                response.json({
                    code: 200,
                    msg: "修改成功~~~"
                });
            } else {
                response.json({
                    code: -200,
                    magg: "修改失败~~~"
                });
            }
        } catch (error) {
            response.json({
                code: -200,
                magg: "修改失败~~~",
                error
            });
        }
    }

    async deleteCart(request,response,next){
        let dparmas=[
            request.body.spid
        ]
        let dsql='UPDATE `shopp` SET `ssum`=0,`sstart`=0,`stotal`=`sprice`*`ssum` WHERE `sid`=? AND `sstart`="1";';
        try {
            let dresult=await db.exec(dsql,dparmas);
            if(dresult && dresult.affectedRows >= 1){
              
                response.json({
                    code:200,
                    msg:"删除成功~~~"
                })
            }else{
                response.json({
                    code:-200,
                    msg:"删除失败1~~~"
                })
            }
        } catch (error) {
            response.json({
                code:-200,
                msg:"删除失败2~~~",
                error
            })
        }
    }

    async giveMoney(request,response,next){
        resposne.send("addCart")
    }
}


module.exports = new CartController();